public class Euler012 {

	public static void main(String[] args) {
		System.out.println("Start");
		Euler012 main = new Euler012();
		int N = 0;
		final int MAX = 500;
		long start = System.nanoTime();
		// triangle number
		int i = 0;
		while (main.numberOfDivisor(N) < MAX) {
			N = N + i;
			i = i + 1;
		}
		System.out.println("Number: " + N + "\tTime" + (System.nanoTime() - start));
		System.out.println("Finish");
		main.printFactor(16);
	}

	private int numberOfDivisor(final int N) {
		int count = 0;
		final int divisor = (int) Math.sqrt(N);
		for (int i = 1; i < divisor; i++) {
			if ((N % i) == 0) {
				count = count + 2;
			}
		}
		// If it is square number
		if (divisor * divisor == N) {
			count = count - 1;
		}
		return count;
	}

	private void printFactor(final int N) {
		int divisor = (int) Math.sqrt(N);
		int i = 2;
		while (i < divisor) {
			if ((divisor % i) == 0) {
				int temp = divisor / i;
				if (isPrime(temp)) {
					System.out.println(temp);
					break;
				} else {
					divisor = temp;
				}
				System.out.println(i);
			} else {
				i = i + 1;
			}
		}
	}

	private boolean isPrime(final int N) {
		for (int i = 2; i <= Math.sqrt(N); i++) {
			if (N % i == 0)
				return false;
		}
		return true;
	}
}
